//
//  XFYStatisticalChart.m
//  MyWallet
//
//  Created by 小肥羊 on 15/8/25.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "XFYStatisticalChart.h"

#define PI 3.14159265358979323846

static inline float radians(double degrees) {
    return degrees * PI / 180;
}

static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color, float radius) {
    CGContextMoveToPoint(ctx, point.x, point.y);
    CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
    CGContextAddArc(ctx, point.x, point.y, radius,  angle_start, angle_end, 0);
    CGContextFillPath(ctx);
}

@implementation XFYCartogram
- (id)init
{
    self = [super init];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

#pragma mark - Set属性
- (void)setContents:(NSMutableArray *)contents
{
    _contents = contents;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGPoint center = CGPointMake(rect.size.width/2, rect.size.height/2);
    float radius = rect.size.width/2;
    CGContextRef ctx = UIGraphicsGetCurrentContext();
//        CGContextClearRect(ctx, rect);
    drawArc(ctx, center, radians(0.0), radians(360), [UIColor groupTableViewBackgroundColor], radius);
    if (self.contents.count > 0) {
        float total = 0;
        for (NSDictionary *dict in self.contents) {
            total += [dict[@"dataValue"] floatValue];
        }
        float unit = 360/total;
        float angle_start = radians(0.0);
        for (NSInteger i=0; i<self.contents.count; i++) {
            float angle_end = angle_start + radians(unit * [self.contents[i][@"dataValue"] floatValue]);
            drawArc(ctx, center, angle_start, angle_end, self.contents[i][@"dataColor"], radius);
            angle_start = angle_end;
        }
    }
}
@end

@implementation XFYStatisticalChart
#pragma mark - 初始化
- (void)initValue
{
    self.dataColumnView = [[UIView alloc] init];
    self.cartogramView = [[XFYCartogram alloc] init];
    [self addSubview:self.dataColumnView];
    [self addSubview:self.cartogramView];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initValue];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initValue];
    }
    return self;
}

#pragma mark - Set属性
- (void)setContents:(NSMutableArray *)contents
{
    _contents = contents;
    NSMutableArray *cartogramData = [[NSMutableArray alloc] init];
    for (NSInteger i=0; i<_contents.count; i++) {
        NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
        [data setValue:_contents[i][@"dataValue"] forKey:@"dataValue"];
        [data setValue:(UIColor *)[StatisticalChartColor objectAtIndex:i] forKey:@"dataColor"];
        [cartogramData addObject:data];
        if (i == 7) {
            //最多为8
            break;
        }
    }
    self.cartogramView.contents = cartogramData;
    [self upViews];
}

#pragma mark - 界面显示
- (void)upViews
{
    float cartogramHeight = self.frame.size.height;
    float dataColumnHeight = cartogramHeight/8;
    //显示数据图
    for (UIView *contentView in [self.dataColumnView subviews])
    {
        [contentView removeFromSuperview];
    }
    [self.dataColumnView setFrame:(CGRect){CGPointZero, CGSizeMake(self.frame.size.width - cartogramHeight, cartogramHeight)}];
    for (NSInteger i=0; i<self.contents.count; i++) {
        UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(2, dataColumnHeight * i + 2, dataColumnHeight - 4, dataColumnHeight - 4)];
        [colorView setBackgroundColor:[StatisticalChartColor objectAtIndex:i]];
        UILabel *dataLabel = [[UILabel alloc] initWithFrame:CGRectMake(dataColumnHeight, dataColumnHeight * i, self.dataColumnView.frame.size.width - dataColumnHeight, dataColumnHeight)];
        [dataLabel setFont:[UIFont systemFontOfSize:9.0]];
        [dataLabel setText:self.contents[i][@"dataName"]];
        [self.dataColumnView addSubview:colorView];
        [self.dataColumnView addSubview:dataLabel];
        if (i == 7) {
            //最多为8
            break;
        }
    }
    //显示统计图
    for (UIView *contentView in [self.cartogramView subviews])
    {
        [contentView removeFromSuperview];
    }
    [self.cartogramView setFrame:(CGRect){CGPointMake(self.dataColumnView.frame.size.width + 6, 6), CGSizeMake(cartogramHeight - 12, cartogramHeight - 12)}];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self upViews];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
